home *** CD-ROM | disk | FTP | other *** search
/ HAKERIS 11 / HAKERIS 11.ISO / linux / system / LinuxConsole 0.4 / linuxconsole0.4install-en.iso / guile0.4.lcm / share / guile / slib / ratize.scm < prev    next >
Encoding:
Text File  |  2004-01-06  |  668 b   |  18 lines

  1. ;;;; "ratize.scm" Find simplest number ratios
  2.  
  3. (define (find-ratio-between x y)
  4.   (define (sr x y)
  5.     (let ((fx (inexact->exact (floor x))) (fy (inexact->exact (floor y))))
  6.       (cond ((>= fx x) (list fx 1))
  7.         ((= fx fy) (let ((rat (sr (/ (- y fy)) (/ (- x fx)))))
  8.              (list (+ (cadr rat) (* fx (car rat))) (car rat))))
  9.         (else (list (+ 1 fx) 1)))))
  10.   (cond ((< y x) (find-ratio-between y x))
  11.     ((>= x y) (list x 1))
  12.     ((positive? x) (sr x y))
  13.     ((negative? y) (let ((rat (sr (- y) (- x))))
  14.              (list (- (car rat)) (cadr rat))))
  15.     (else '(0 1))))
  16. (define (find-ratio x e) (find-ratio-between (- x e) (+ x e)))
  17. (define (rationalize x e) (apply / (find-ratio x e)))
  18.